<template>
  <div class="course-wrapper">
    <div class="course-header-wrapper">
      <div class="course-header-search-wrapper">
        <el-input class="course-header-search-input"
                  prefix-icon="el-icon-search"
                  v-model="courseName"
                  placeholder="请输入课程名称进行搜索"></el-input>
        <el-button class="course-header-search-button"
                   icon="el-icon-search"
                   @click=""
                   type="primary">搜索</el-button>
      </div>
      <div class="course-header-operation-wrapper">
        <el-button class="course-header-add-button"
                   @click="showAddCourseDialog"
                   type="primary"
                   icon="el-icon-plus">添加课程</el-button>
        <el-button class="course-header-delete-batch-button"
                   type="danger"
                   @click="deleteCoursesByBatch"
                   :disabled="this.multipleSelections.length === 0"
                   icon="el-icon-delete">批量删除</el-button>
      </div>
    </div>
    <div class="course-table-wrapper">
      <el-table
              :data="courses"
              v-loading="loading"
              element-loading-text="拼命加载中"
              @selection-change="handleSelectionChange"
              stripe
              style="width: 100%">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="id"
                label="编号"
                width="100">
        </el-table-column>
        <el-table-column
                prop="name"
                label="课程名称"
                width="180">
        </el-table-column>
        <el-table-column
                prop="type"
                label="类型"
                width="120">
        </el-table-column>
        <el-table-column
                prop="score"
                label="学分"
                width="80">
        </el-table-column>
        <el-table-column
                prop="lesson"
                label="课时"
                width="120">
        </el-table-column>
        <el-table-column label="授课老师"
                         width="80">
          <template slot-scope="scope">
            <el-popover
                    placement="left"
                    title="授课老师"
                    @show="showPop(scope.row)"
                    trigger="hover">
              <el-tag style="display: block;width: 70%;margin-top: 5px"
                      type="success"
                      v-for="teacher in selectedTeachers"
                      :key="teacher.id">{{teacher.name}}</el-tag>
              <el-button size="mini"
                         slot="reference"
                         type="success">查看</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini"
                       @click="showEditCourseDialog(scope.row)"
                       type="primary">编辑</el-button>
            <el-button type="danger"
                       @click="deleteCourse(scope.row)"
                       size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog class="dialog"
               width="25%"
               :title="title"
               :visible.sync="dialogFormVisible">
      <div class="dialog-form-wrapper">
        <el-form class="dialog-form"
                 :rules="rules"
                 :model="course"
                 ref="courseForm">
          <el-row>
            <el-col :span="24">
              <el-form-item label="课程名称:" prop="name">
                <el-input v-model="course.name"
                          prefix-icon="el-icon-edit"
                          placeholder="请输入课程名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="类型:" prop="type">
                <el-select v-model="course.type"
                           placeholder="请选择课程类型">
                  <el-option
                          v-for="(item, index) in courseTypes"
                          :key="index"
                          :label="item"
                          :value="item">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="学分:" prop="score">
                <el-input v-model="course.score"
                          prefix-icon="el-icon-edit"
                          placeholder="请输入课程学分"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="课时:" prop="lesson">
                <el-input v-model="course.lesson"
                          prefix-icon="el-icon-edit"
                          placeholder="请输入课时"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogCourse">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "EduCourse",
    data() {
      return {
        courses: [],
        courseName: '',
        multipleSelections: [],
        loading: false,
        title: '',
        dialogFormVisible: false,
        courseTypes: ['必修', '选修'],
        selectedTeachers: [],
        course: {
          id: null,
          name: '',
          type: '',
          score: null,
          lesson: null
        },
        rules: {
          name: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
          type: [{ required: true, message: '请选择课程类型', trigger: 'blur' }],
          score: [{ required: true, message: '请输入课程学分', trigger: 'blur' }],
          lesson: [{ required: true, message: '请输入课时', trigger: 'blur' }]
        }
      }
    },
    mounted() {
      this.getCourses()
    },
    methods: {
      showPop(data) {
        this.selectedTeachers = []
        this.getTeachersByCourse(data)
      },
      //  根据课程id获取老师
      getTeachersByCourse(data) {
        this.getRequest('/edumanagement/course/teacher/' + data.id).then(resp => {
          if (resp) {
            this.selectedTeachers = resp
          }
        })
      },
      //  获取所有课程
      getCourses() {
        this.loading = true
        this.getRequest('/edumanagement/course/').then(resp => {
          if (resp) {
            this.courses = resp
            this.loading = false
          }
        })
      },
      //  展示添加课程dialog
      showAddCourseDialog() {
        this.title = '添加课程'
        this.course = {
          id: null,
          name: '',
          type: '',
          score: null,
          lesson: null
        }
        this.dialogFormVisible = true
      },
      //  批量删除课程
      deleteCoursesByBatch() {
        this.$confirm('此操作将永久删除这['+ this.multipleSelections.length +']个课程, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = '?'
          this.multipleSelections.forEach(item => {
            ids += 'ids=' + item.id + '&'
          })
          this.deleteRequest('/edumanagement/course/' + ids).then(resp => {
            if (resp) {
              this.getCourses()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  获取被选中的课程
      handleSelectionChange(data) {
        this.multipleSelections = data
      },
      //  展示更新课程dialog
      showEditCourseDialog(data) {
        this.title = '更新课程'
        //  this.updatePos = data 这个直接赋值会导致bug，输入框与table里的数据同步变化
        //  Object.assign(target, source1, source2)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target)
        Object.assign(this.course, data)
        this.dialogFormVisible = true
      },
      //  删除课程
      deleteCourse(data) {
        this.$confirm('此操作将永久删除['+ data.name +']课程, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/edumanagement/course/' + data.id).then(resp => {
            if (resp) {
              this.getCourses()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  添加或更新课程
      dialogCourse() {
        if (this.course.id) {
          //  更新课程
          this.$refs['courseForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.putRequest('/edumanagement/course/', this.course).then(resp => {
                if (resp) {
                  this.getCourses()
                }
              })
            }
          })
        } else {
          //  添加课程
          this.$refs['courseForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.postRequest('/edumanagement/course/', this.course).then(resp => {
                if (resp) {
                  this.getCourses()
                }
              })
            }
          })
        }
      }
    }
  }
</script>

<style lang="less">

  .course-wrapper {
    width: 90%;
    margin-top: 20px;
    margin-left: 40px;

    .course-header-wrapper {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      box-sizing: border-box;
      padding: 20px 20px;
      /*background-color: #EBEEF5;*/
      height: 80px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;

      .course-header-search-wrapper {
        display: flex;

        .course-header-search-input {
          margin-right: 10px;
        }

        .course-header-search-button {
          height: min-content;
        }

      }

      .course-header-operation-wrapper {

        .course-header-add-button {

        }

        .course-header-delete-batch-button {

        }
      }
    }

    .course-table-wrapper {
      margin-top: 20px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;
      padding: 5px 20px 20px 20px;
    }
  }
</style>